package com.dingshang.dsapp.service;

import com.dingshang.dsapp.dto.SheJiFuWuDTO;
import com.dingshang.dsapp.dto.ShejishiDTO;
import com.dingshang.dsapp.dto.SjsDTO;
import com.dingshang.dsapp.dto.WebInfoDTO;
import com.dingshang.dsapp.entity.KeyValueWithBLOBs;
import com.dingshang.dsapp.exception.BizException;
import com.dingshang.dsapp.holder.PageHolder;
import com.dingshang.dsapp.mapper.KeyValueMapper;
import com.dingshang.dsapp.mapper.NavigationMapper;
import com.dingshang.dsapp.mapper.PictureMapper;
import com.dingshang.dsapp.vo.WebInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BoService {

    @Value("${file.image.path}")
    protected String filePath;

    @Value("${file.image.url}")
    protected String fileUrl;

    @Autowired
    private KeyValueMapper keyValueMapper;


    @Autowired
    private NavigationMapper navigationMapper;


    @Autowired
    private PictureMapper pictureMapper;

    //Keyvalue type 1
    public void saveWebInfo(WebInfoVO webInfoVO) {

        keyValueMapper.deleteKeyValue(1);

        KeyValueWithBLOBs keyValue = new KeyValueWithBLOBs();

        keyValue.setValueCase1(webInfoVO.getKeyword());
        keyValue.setValueCase2(webInfoVO.getPageTitle());
        keyValue.setValueCase3(webInfoVO.getIconUrl());
        keyValue.setValueCase4(webInfoVO.getBeianhao());
        keyValue.setValueType(1);

        keyValueMapper.insert(keyValue);

    }

    public WebInfoDTO getWebInfo() {
        WebInfoDTO dto = new WebInfoDTO();

        KeyValueWithBLOBs keyValue = keyValueMapper.getKeyValue(1);
        if (keyValue == null) {
            return dto;
        }

        dto.setId(keyValue.getId());
        dto.setKeyword(keyValue.getValueCase1());
        dto.setPageTitle(keyValue.getValueCase2());
        dto.setIconUrl(keyValue.getValueCase3());
        dto.setBeianhao(keyValue.getValueCase4());
        dto.setIconPicurl(fileUrl + keyValue.getValueCase3());

        return dto;
    }

    //2-设计师 3-文章 5-别墅 6-平层公寓 7-私人定制 8-美学研究 9-设计服务 10-施工服务
    public void saveShejishi(ShejishiDTO dto) {

        if (dto.getId() != 0) {
            keyValueMapper.deleteByPrimaryKey(dto.getId());
        }

        KeyValueWithBLOBs keyValue = new KeyValueWithBLOBs();

        if (null == dto.getWzcontext() || "".equals(dto.getWzcontext())) {

        } else {
            KeyValueWithBLOBs keyValue2 = new KeyValueWithBLOBs();
            keyValue2.setValueCase1(dto.getWztitle());
            keyValue2.setValueText(dto.getWzcontext());
            keyValue2.setValueType(3);

            keyValueMapper.insert(keyValue2);

            keyValue.setHref1(String.valueOf(keyValue2.getId()));
        }


        keyValue.setValueCase1(dto.getBigTitle());
        keyValue.setValueCase2(dto.getTitle1());
        keyValue.setValueCase3(dto.getTitle2());
        keyValue.setValueCase4(dto.getTitle3());
        keyValue.setPicUrl(dto.getPicUrl());
        keyValue.setValueType(dto.getValueType());

        keyValueMapper.insert(keyValue);

    }

    public ShejishiDTO getShejishi(int valueType) {
        ShejishiDTO dto = new ShejishiDTO();

        KeyValueWithBLOBs kv = keyValueMapper.getKeyValue(valueType);
        if (null == kv) {
            return dto;
        }

        dto.setBigTitle(kv.getValueCase1());
        dto.setTitle1(kv.getValueCase2());
        dto.setTitle2(kv.getValueCase3());
        dto.setTitle3(kv.getValueCase4());
        dto.setPicName(kv.getPicUrl());
        dto.setPicUrl(fileUrl + kv.getPicUrl());
        dto.setWzId(null == kv.getHref1() ? 0 : Integer.parseInt(kv.getHref1()));
        dto.setId(kv.getId());

        KeyValueWithBLOBs wz = keyValueMapper.selectByPrimaryKey(Integer.valueOf(null == kv.getHref1()?"0" : kv.getHref1()));
        if (wz == null) {
            return dto;
        }

        dto.setWztitle(wz.getValueCase1());
        dto.setWzcontext(wz.getValueText());

        return dto;
    }

    public List<ShejishiDTO> listShejishi(int valueType) {

        List<ShejishiDTO> dtoList = new ArrayList<>();
        List<KeyValueWithBLOBs> keyValueWithBLOBs = keyValueMapper.listKeyValue(valueType);
        for (KeyValueWithBLOBs kv : keyValueWithBLOBs) {
            ShejishiDTO dto = new ShejishiDTO();
            dtoList.add(dto);

            dto.setBigTitle(kv.getValueCase1());
            dto.setTitle1(kv.getValueCase2());
            dto.setTitle2(kv.getValueCase3());
            dto.setTitle3(kv.getValueCase4());
            dto.setPicName(kv.getPicUrl());
            dto.setPicUrl(fileUrl + kv.getPicUrl());
            dto.setWzId(null == kv.getHref1() ? 0 : Integer.parseInt(kv.getHref1()));
            dto.setId(kv.getId());

            KeyValueWithBLOBs wz = keyValueMapper.selectByPrimaryKey(Integer.valueOf(null == kv.getHref1()?"0" : kv.getHref1()));
            if (wz == null) {
                continue;
            }

            dto.setWztitle(wz.getValueCase1());
            dto.setWzcontext(wz.getValueText());

        }

        return dtoList;
    }

    public void deleteKeyValue(int id) {
        keyValueMapper.deleteByPrimaryKey(id);
    }

    //姓名 小区 电话 面积 留言

    //装修案例 10   zxStyle: 0-现在 1-新中式 2-美式 3-欧式
    public void addAnli(String bigTitle, String title1, String title2,
                        String picUrl, String zxStyle, String shejishiId,
                        String content) {

        KeyValueWithBLOBs keyValue = new KeyValueWithBLOBs();
        keyValue.setPicUrl(picUrl);
        keyValue.setValueType(10);
        keyValue.setPicTitle(bigTitle);
        keyValue.setValueCase1(title1);
        keyValue.setValueCase2(title2);
        keyValue.setValueCase3(shejishiId);
        keyValue.setValueCase4(zxStyle);
        keyValue.setValueText(content);
        keyValue.setHref2(1);

        KeyValueWithBLOBs kv = keyValueMapper.selectByPrimaryKey(Integer.valueOf(shejishiId));

        keyValue.setValueText2(kv.getPicTitle());

        keyValueMapper.insert(keyValue);

    }

    public List<KeyValueWithBLOBs> listAnli(String zxStyle) {
        List<KeyValueWithBLOBs> keyValueWithBLOBs = keyValueMapper.listAnli(zxStyle, 10);
        for (KeyValueWithBLOBs kv : keyValueWithBLOBs) {
            kv.setPicUrl(fileUrl + kv.getPicUrl());
        }

        return keyValueWithBLOBs;
    }

    //服务 11
    public void saveService(String title1, String bigTitle,
                            String wzcontext, String serverStyle) {

        KeyValueWithBLOBs keyValue = new KeyValueWithBLOBs();
        keyValue.setValueType(11);
        keyValue.setPicTitle(bigTitle);
        keyValue.setValueCase1(title1);
        keyValue.setValueCase4(serverStyle);
        keyValue.setValueText(wzcontext);

        keyValueMapper.insert(keyValue);
    }

    public List<KeyValueWithBLOBs> listServer(String serverStyle) {

        List<KeyValueWithBLOBs> keyValueWithBLOBs = keyValueMapper.listServer(serverStyle, 11);
        for (KeyValueWithBLOBs kv : keyValueWithBLOBs) {
            kv.setPicUrl(fileUrl + kv.getPicUrl());
        }

        return keyValueWithBLOBs;
    }

    public KeyValueWithBLOBs loadKv(int id) {
        return keyValueMapper.selectByPrimaryKey(id);
    }

    //设计师 12
    public void addShejishi(String realName, String anliNum, String jingyan,
                            String zhiwei, String picUrl, String sjStyle, String jianjie) {

        KeyValueWithBLOBs keyValue = new KeyValueWithBLOBs();
        keyValue.setValueType(12);
        keyValue.setPicTitle(realName);
        keyValue.setValueCase1(anliNum);
        keyValue.setValueCase2(jingyan);
        keyValue.setValueCase3(zhiwei);
        keyValue.setPicUrl(picUrl);
        keyValue.setValueCase4(sjStyle);
        keyValue.setValueText(jianjie);
        keyValue.setHref2(1);

        keyValueMapper.insert(keyValue);
    }

    public List<KeyValueWithBLOBs> listShejishi(String sjStyle) {

        List<KeyValueWithBLOBs> keyValueWithBLOBs = keyValueMapper.listShejishi(sjStyle, 12);
        for (KeyValueWithBLOBs kv : keyValueWithBLOBs) {
            kv.setPicUrl(fileUrl + kv.getPicUrl());
        }

        return keyValueWithBLOBs;
    }

    //关于我们 13
    public void addAbout(String context, String aboutStyle, String bigTitle, String title1) {

        keyValueMapper.delAbout(13, aboutStyle);

        KeyValueWithBLOBs keyValue = new KeyValueWithBLOBs();
        keyValue.setValueType(13);
        keyValue.setPicTitle(bigTitle);
        keyValue.setValueCase1(title1);
        keyValue.setValueCase4(aboutStyle);
        keyValue.setValueText(context);

        keyValueMapper.insert(keyValue);
    }

    public List<KeyValueWithBLOBs> listAbout(String aboutStyle) {

        List<KeyValueWithBLOBs> keyValueWithBLOBs = keyValueMapper.listAbout(aboutStyle, 13);

        return keyValueWithBLOBs;
    }

    //客户报价 14
    public void baojia(String phone, String realName,
                       String loupan, String mianji) {

        KeyValueWithBLOBs keyValue = new KeyValueWithBLOBs();

        keyValue.setValueType(14);
        keyValue.setValueCase1(phone);
        keyValue.setValueCase2(realName);
        keyValue.setValueCase3(loupan);
        keyValue.setValueCase4(mianji);

        keyValueMapper.insert(keyValue);
    }

    public PageHolder listBaojia(int currentPage) {
        int totalNumber = keyValueMapper.countBaojia(14);
        PageHolder pageHolder = PageHolder.build(currentPage, totalNumber);

        List<KeyValueWithBLOBs> keyValueWithBLOBs = keyValueMapper.listBaojia(14, pageHolder.getOffset(), PageHolder.prePageNum);

        pageHolder.setData(keyValueWithBLOBs);

        return pageHolder;
    }

    //修改密码 15
    public void modifyPwd(String pwd, String confimPwd) {

        KeyValueWithBLOBs keyValue = keyValueMapper.getKeyValue(15);

        if (null == pwd || "".equals(pwd)) {
            throw new BizException("请输入新密码");
        }

        if (null == confimPwd || "".equals(confimPwd)) {
            throw new BizException("请输入确认密码");
        }

        if (!confimPwd.equals(pwd)) {
            throw new BizException("两次输入的密码不相同");
        }

        keyValue.setValueCase1(pwd);
        int rows = keyValueMapper.updateByPrimaryKeySelective(keyValue);
        if (1 != rows) {
            throw new BizException("修改密码失败");
        }
    }

    public KeyValueWithBLOBs login(String username, String pwd) {
        KeyValueWithBLOBs keyValue = keyValueMapper.getKeyValue(15);
        if (null == keyValue) {
            throw new BizException("用户不存在或者密码错误");
        }

        if (!keyValue.getValueCase1().equals(pwd)) {
            throw new BizException("用户不存在或者密码错误");
        }

        return keyValue;
    }

    public List<KeyValueWithBLOBs> loadShejishi() {
        return keyValueMapper.listAllShejishi(12);
    }


    public PageHolder zhuangshianli(int page, String zxStyle) {

        int total = keyValueMapper.countAppAnli(zxStyle, 10);
        PageHolder pageHolder = PageHolder.build(page, total);

        List<KeyValueWithBLOBs> list = keyValueMapper.listAppAnli(zxStyle, 10, pageHolder.getOffset(), 2);
        for (KeyValueWithBLOBs kv : list) {
            kv.setPicUrl(fileUrl + kv.getPicUrl());
            KeyValueWithBLOBs sjs = keyValueMapper.selectByPrimaryKey(Integer.valueOf(kv.getValueCase3()));

            //设计师头像
            kv.setValueText(fileUrl + sjs.getPicUrl());
        }

        pageHolder.setData(list);

        return pageHolder;
    }

    public KeyValueWithBLOBs getFuwu(String serverStyle) {
        KeyValueWithBLOBs fuwu = keyValueMapper.getFuwu(serverStyle, 11);
        System.out.println(">>>>>>>" + fuwu.getPicTitle());

        return fuwu;
    }

    public PageHolder listAppShejishi(String sjStyle, int page) {
        int total = keyValueMapper.countShejishi(sjStyle, 12);
        PageHolder pageHolder = PageHolder.build(page, total);

        List<KeyValueWithBLOBs> list = keyValueMapper.listappShejishi(sjStyle, 12, pageHolder.getOffset(), PageHolder.prePageNum);
        for (KeyValueWithBLOBs kv : list) {
            kv.setPicUrl(fileUrl + kv.getPicUrl());
        }

        pageHolder.setData(list);

        return pageHolder;
    }

    public KeyValueWithBLOBs getAbout(String aboutStyle) {
        return keyValueMapper.getAbout(aboutStyle, 13);
    }

    public KeyValueWithBLOBs getValueById(int anliId) {
        KeyValueWithBLOBs keyValueWithBLOBs = keyValueMapper.selectByPrimaryKey(anliId);
        keyValueWithBLOBs.setPicUrl(fileUrl + keyValueWithBLOBs.getPicUrl());
        return keyValueWithBLOBs;
    }

    public KeyValueWithBLOBs getValue4Common(int id) {
        KeyValueWithBLOBs keyValueWithBLOBs = keyValueMapper.selectByPrimaryKey(id);
        return keyValueWithBLOBs;
    }

    public SjsDTO loadShejishiDetail(int sjsId) {
        SjsDTO sjsDTO = new SjsDTO();

        KeyValueWithBLOBs sjs = keyValueMapper.selectByPrimaryKey(sjsId);
        sjs.setPicUrl(fileUrl + sjs.getPicUrl());

        sjsDTO.setId(sjs.getId());

        sjsDTO.setPicUrl(sjs.getPicUrl());
        sjsDTO.setRealName(sjs.getPicTitle());
        sjsDTO.setZhiwei(sjs.getValueCase3());
        sjsDTO.setJianjie(sjs.getValueText());
        List<SjsDTO.AnliInfo> anliInfos = new ArrayList<>();

        sjsDTO.setNalis(anliInfos);

        List<KeyValueWithBLOBs> anlis = keyValueMapper.listShejishiAnli(10, sjsId);
        for (KeyValueWithBLOBs anli : anlis) {
            SjsDTO.AnliInfo info = new SjsDTO.AnliInfo();
            anliInfos.add(info);

            info.setBigTitle(anli.getPicTitle());
            info.setId(anli.getId());
            info.setPicUrl(fileUrl + anli.getPicUrl());
            info.setTitle1(anli.getValueCase1());
        }

        return sjsDTO;

    }


    public void modifyShejishi(int id, String realName, String anliNum, String jingyan,
                            String zhiwei, String picUrl, String sjStyle, String jianjie) {

        KeyValueWithBLOBs keyValue = keyValueMapper.selectByPrimaryKey(id);
        keyValue.setPicTitle(realName);
        keyValue.setValueCase1(anliNum);
        keyValue.setValueCase2(jingyan);
        keyValue.setValueCase3(zhiwei);
        keyValue.setPicUrl(picUrl);
        keyValue.setValueCase4(sjStyle);
        keyValue.setValueText(jianjie);

        keyValueMapper.updateByPrimaryKeySelective(keyValue);
    }

    public void modifyAnli(int id, String bigTitle, String title1, String title2,
                           String picUrl, String zxStyle, String shejishiId, String content) {

        KeyValueWithBLOBs keyValue = keyValueMapper.selectByPrimaryKey(id);

        keyValue.setPicUrl(picUrl);
        keyValue.setPicTitle(bigTitle);
        keyValue.setValueCase1(title1);
        keyValue.setValueCase2(title2);
        keyValue.setValueCase3(shejishiId);
        keyValue.setValueCase4(zxStyle);
        keyValue.setValueText(content);

        KeyValueWithBLOBs kv = keyValueMapper.selectByPrimaryKey(Integer.valueOf(shejishiId));

        keyValue.setValueText2(kv.getPicTitle());
        keyValueMapper.updateByPrimaryKeySelective(keyValue);

    }

    /**
     * 修改案例和设计师顺序
     * @param id
     * @param sxValue
     */
    public void modifySx(int id, int sxValue) {
        int rows = keyValueMapper.modifyShunXu(id, sxValue);
        if (1 != rows) {
            throw new BizException("顺序修改失败");
        }
    }

    /**
     *
     * @return
     */
    public PageHolder listAllAppAnli(int valueType, int page) {
        int total = keyValueMapper.countAllAppStucf(valueType);
        PageHolder pageHolder = PageHolder.build(page, total);


        if (valueType == 10) {
            List<KeyValueWithBLOBs> list = keyValueMapper.listAllAppStucf(valueType, pageHolder.getOffset(), 2);
            for (KeyValueWithBLOBs kv : list) {
                kv.setPicUrl(fileUrl + kv.getPicUrl());
                KeyValueWithBLOBs sjs = keyValueMapper.selectByPrimaryKey(Integer.valueOf(kv.getValueCase3()));

                //设计师头像
                kv.setValueText(fileUrl + sjs.getPicUrl());
            }

            pageHolder.setData(list);
        } else {
            List<KeyValueWithBLOBs> list = keyValueMapper.listAllAppStucf(12, pageHolder.getOffset(), PageHolder.prePageNum);
            for (KeyValueWithBLOBs kv : list) {
                kv.setPicUrl(fileUrl + kv.getPicUrl());
            }

            pageHolder.setData(list);
        }


        return pageHolder;
    }


}
